package app.rci.action.user;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.util.Log;
import app.rci.action.common.BaseActionBean;
import app.rci.domain.UserData;
import app.rci.model.UserModel;
import app.rci.model.UserModelExample;
import app.rci.service.UsersService;
import app.rci.util.JSONUtil;

public class UsersActionBean extends BaseActionBean {
	private static final Log log        = Log.getInstance(UsersActionBean.class);
	@SpringBean UsersService userService;
	
	private UserModel data;
	private UserModelExample userEx;
	private List<UserModel> listUser;
	private UserData param;
	
	@DefaultHandler
	public Resolution show(){
		return new ForwardResolution("/jsp/content/cGridUsers.jsp");
	}
	
	public Resolution userGrid(){
		return new ForwardResolution("/jsp/content/cGridUsers.jsp");
	}	
	
	public Resolution getDataUsers(){
		log.debug("+++++++++++++++ getDataUsers +++++++++++++++");
		JSONUtil json= new JSONUtil();
		getParam().setStart(getContext().getRequest().getParameter("start"));
		getParam().setLimit(getContext().getRequest().getParameter("limit"));
		List<UserModel> listResultsModel= (List<UserModel>) userService.getTotalRecord(getUserEx());
		List<UserModel> listModel= (List<UserModel>) userService.getDataUser(getUserEx(),getParam());
		json.addData("results", listResultsModel.size());
		json.addData("rows", listModel);
		
		return new StreamingResolution("application/json", json.serialize());
	}

	public Resolution saveUser(){
		log.debug("+++++++++++++++ testLogin +++++++++++++++");
		JSONUtil json= new JSONUtil();
		
		int results= userService.saveDataUser(getUserEx(), getData());
		log.debug("++++++++++++++ int results = "+results);
		json.addData("success", true);
		return new StreamingResolution("application/json", json.serialize());
	}
	
	public Resolution deleteUser(){
		log.debug("+++++++++++++++ delete process +++++++++++++++");
		String tmpDelete= getContext().getRequest().getParameter("tmpDelete");
		String[] listDelete= tmpDelete.split(",");
		for(int i=0;i<listDelete.length;i++){
			log.debug("+++++++++ String-"+i+" = "+listDelete[i].trim());
			int idDelete= Integer.parseInt(listDelete[i].trim());
			int results= userService.delDataUser(idDelete);	
			log.debug("+++++++++ results = "+results);
		}
		
		JSONUtil json= new JSONUtil();		
		json.addData("success", true);
		return new StreamingResolution("application/json", json.serialize());
	}
	
	public Resolution coba(){
		return new ForwardResolution("/jsp/content/cGridUsers.jsp");
	}
	
	public UserModel getData() {
		if(data==null)
			data= new UserModel();
		return data;
	}

	public void setData(UserModel data) {
		this.data = data;
	}

	public UserModelExample getUserEx() {
		if(userEx==null)
			userEx= new UserModelExample();
		return userEx;
	}

	public void setUserEx(UserModelExample userEx) {
		this.userEx = userEx;
	}

	public List<UserModel> getListUser() {
		if(listUser==null)
			listUser= new ArrayList<UserModel>();
		return listUser;
	}

	public void setListUser(List<UserModel> listUser) {
		this.listUser = listUser;
	}

	public UserData getParam() {
		if(param == null)
			param= new UserData();
		return param;
	}

	public void setParam(UserData param) {
		this.param = param;
	}
}
